package com.example.sunsg.okhttptest.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by landmark on 2015/12/2 20:35
 */
public class TorchUtils {
    protected static final boolean DEBUG = true;
    public static final String TAG = "TorchUtils";


    /**
     * 6.0以及以上手机请求获取相机权限
     *
     * @param activity
     */
    public static void requestPermissions(Activity activity) {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, 0);
    }

    /**
     * 判断6.0以及以上手机相机权限是否被禁用
     *
     * @param context
     * @return
     */
    public static boolean checkSelfPermission(Context context) {
        int permission = PermissionChecker.checkSelfPermission(context, Manifest.permission.CAMERA);
        if (DEBUG) {
            Log.i(TAG, "permission = " + permission);
        }
        return permission == PermissionChecker.PERMISSION_DENIED_APP_OP;
    }

    /**
     * 获取相机权限类型
     *
     * @param context
     * @return
     */
    public static int getCamerPermission(Context context) {
        return PermissionChecker.checkSelfPermission(context, Manifest.permission.CAMERA);
    }



//    public static void skipToPermissionManager(Context context) {
//        if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.HUAWEI)) {
//            skipToHuaweiPermission(context);
//        } else if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.XIAOMI)) {
//            skipToXiaomiPermission(context);
//        } else if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.MEIZU)) {
//            skipToMeizuPermission(context);
//        } else if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.SAMSUNG)) {
//            skipSamsungPermission(context);
//        } else if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.LENOVO)) {
//            AppUtils.showAppDetails(context, AppEnv.PKGNAME);
//        }
//    }

    private static void skipToHuaweiPermission(Context context) {
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClassName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
            context.startActivity(intent);
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
    }

//    private static void skipToXiaomiPermission(Context context) {
//        try {
//            Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setClassName("com.miui.securitycenter",
//                    "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
//            intent.putExtra("extra_pkgname", AppEnv.PKGNAME);
//            context.startActivity(intent);
//        } catch (Exception e) {
//            if (DEBUG) {
//                e.printStackTrace();
//            }
//        }
//    }

    private static void skipToMeizuPermission(Context context) {
        try {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
    }

    private static void skipSamsungPermission(Context context) {
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClassName("com.sec.android.app.capabilitymanager",
                    "com.sec.android.app.cm.ui.CMApplicationListActivity");
            context.startActivity(intent);
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
    }

    public static boolean isAndroid23(Context context) {
        if (Build.VERSION.SDK_INT >= 23 && TorchUtils.checkSelfPermission(context)) {
            if (DEBUG) {
                Log.i(TAG, "the phone is android 6.0 or above");
            }
            return true;
        }
        return false;
    }

//    public static boolean isAdaptiveModels() {
//        if (ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.HUAWEI) || ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.XIAOMI) || ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.SAMSUNG) || ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.MEIZU)
//                || ManufacturerUtil.isAdaptivePhone(ManufacturerUtil.LENOVO)) {
//            if (DEBUG) {
//                Log.i(TAG, "the phone is adaptive models");
//            }
//            return true;
//        }
//        return false;
//    }

//    /**
//     * 相机权限授权弹窗
//     *
//     * @param activity
//     */
//    public static void showAuthorizeDialog(final Activity activity) {
//        Statistician2.dbLog(StatKey.STATISTIC_TORCH_GUIDE_DIALOG, Build.MODEL, "");
//        final SimpleDialog dialog = new SimpleDialog(activity);
//        dialog.setDialogTitle(R.string.notify_guide_torch_title);
//        dialog.setDialogMessage(R.string.notify_guide_torch_message);
//        dialog.setButtonText(
//                LocaleManager.getInstance().getString(R.string.notify_guide_torch_authorize).toUpperCase(),
//                LocaleManager.getInstance().getString(R.string.cancel).toUpperCase());
//        dialog.setButtonOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TorchUtils.isAndroid23(activity)) {
//                    TorchUtils.requestPermissions(activity);
//                } else {
//                    TorchUtils.skipToPermissionManager(activity);
//                }
//                Statistician2.dbLog(StatKey.STATISTIC_TORCH_GUIDE_AUTHORIZE);
//                DialogUtils.dismissDialog(dialog);
//            }
//        }, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DialogUtils.dismissDialog(dialog);
//            }
//        });
//        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                if (DEBUG) {
//                    Log.i(TAG, "dialog onDismiss");
//                }
//                activity.finish();
//            }
//        });
//        DialogUtils.showDialog(dialog);
//    }


    /**
     * 是否需要申请权限
     */
    public static boolean shouldRequestStorageAuthorize(final Activity activity) {
        int SDK_INT = Build.VERSION.SDK_INT;

        //6.0或者以后
        if (SDK_INT >= 23) {
            boolean hasPermission = (PermissionChecker.checkSelfPermission(activity,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);

            //没有得到SD卡权限
            if (!hasPermission) {
                return true;
            }
        }
        return false;
    }

    //在6.0的时候请求写文件的权限
    private static final int REQUEST_WRITE_STORAGE = 112;

//    /**
//     * 6.0以后储存的权限
//     */
//    public static void showStorageAuthorizeDialog(final Activity activity) {
//        Statistician2.dbLog(StatKey.STATISTIC_TORCH_GUIDE_DIALOG, Build.MODEL, "");
//        final SimpleDialog dialog = new SimpleDialog(activity);
//        dialog.setDialogTitle(R.string.notify_guide_torch_title);
//        dialog.setDialogMessage(R.string.notify_guide_torch_message);
//        dialog.setButtonText(
//                LocaleManager.getInstance().getString(R.string.notify_guide_torch_authorize).toUpperCase(),
//                LocaleManager.getInstance().getString(R.string.cancel).toUpperCase());
//        dialog.setButtonOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ActivityCompat.requestPermissions(activity,
//                        new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
//                        REQUEST_WRITE_STORAGE);
//
//                DialogUtils.dismissDialog(dialog);
//            }
//        }, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DialogUtils.dismissDialog(dialog);
//            }
//        });
//        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                if (DEBUG) {
//                    Log.i(TAG, "dialog onDismiss");
//                }
////                activity.finish();
//            }
//        });
//        DialogUtils.showDialog(dialog);
//
//    }
    public static boolean mIsFirstRequestCameraPermission = true;
    public  static void requestCameraPermissions(Activity activity, int cameraRequestCode){

            if(shouldShowRequestPermissionRationaleCamera(activity)){
                //申请相机权限
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.CAMERA}, cameraRequestCode);
                Toast.makeText(activity,"不是第一次授权kuagn", Toast.LENGTH_SHORT).show();
            }else{
                if(mIsFirstRequestCameraPermission){
                    //申请相机权限
                    ActivityCompat.requestPermissions(activity,
                            new String[]{Manifest.permission.CAMERA}, cameraRequestCode);
                    mIsFirstRequestCameraPermission = false;
                    Toast.makeText(activity,"第一次授权框", Toast.LENGTH_SHORT).show();
                }else {
                    //权限对话框不在提示 提示用户手动开启
                    Toast.makeText(activity,"提示用户手动开启", Toast.LENGTH_SHORT).show();
                }

            }
    }

    public static boolean shouldShowRequestPermissionRationaleCamera(Activity activity){
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA);
    }

    /**
     * 判断6.0以及以上的手机是否有相机权限
     *
     * @param context
     * @return
     */
    public static boolean isCameraPermissionGranted(Context context) {
        int permission = PermissionChecker.checkSelfPermission(context, Manifest.permission.CAMERA);
        if (DEBUG) {
            Log.i(TAG, "permission = " + permission);
        }
        boolean isGranted = permission == PermissionChecker.PERMISSION_GRANTED;
        if(isGranted){
            mIsFirstRequestCameraPermission = true;
        }
        return isGranted;
    }

}

